#include "stdio.h"
#include "signal.h"
#include <sys/stat.h>


static void* sig_handle_main(void *arg)
{
    int rv=-1;
    int new_sig;
    sigset_t set;
    sigemptyset(&set);
    sigaddset(&set, SIGINT);
    sigaddset(&set, SIGQUIT);
    while(1){
        rv = sigwait(&set, &new_sig);
        if (rv == -1) {
            perror("sigwait error");
            return 1;
        }else{
            switch (new_sig){
                case SIGINT:
                    printf("sigwait SIGINT\n");
                    break;
                case SIGQUIT:
                    printf("sigwait SIGQUIT\n");
                    return 0;
                    break;
                default:
                    printf("sigwait unknown signal\n");
                    break;
            }
        }
    }
    return NULL;
}
static void* th1_main(void *arg)
{
    
    while(1){
        usleep(1000*1000);
    }
    return NULL;
}


int main()
{
    int rv=0;
    // 新增sigaction配置
    /*
     struct sigaction {
               void     (*sa_handler)(int);
               void     (*sa_sigaction)(int, siginfo_t *, void *);
               sigset_t   sa_mask;
               int        sa_flags;
               void     (*sa_restorer)(void);
           };
    */
    sigset_t allSigs;
    sigfillset(&allSigs);
    if (sigprocmask(SIG_SETMASK, &allSigs, NULL) == -1){
		perror("sigprocmask");
        return -1;
    }

    pthread_t th1;
    rv= pthread_create(&th1,NULL,th1_main,NULL);
    if(rv ){

    }
    pthread_t sig_thread;
    rv= pthread_create(&sig_thread,NULL,sig_handle_main,NULL);
    if(rv ){

    }
     if (pthread_detach(sig_thread) != 0) {
        // 错误处理
    }


    pthread_join(th1);

    return 0;
}